import express from 'express';
import multer from 'multer';
import path from 'path';
import { v4 } from 'uuid';

const uploadRouter = express.Router();

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    const url = path.resolve('./', '../assest');
    cb(null, url);
  },
  filename: function (req, file, cb) {
    const uuid = v4();
    cb(null, uuid + '.jpg');
  },
});
const upload = multer({
  storage: storage,
});

uploadRouter.post('/uploadImg', upload.single('uploadImg'), (req, res) => {
  console.log(req.file);
  res.send({
    resCode: 200,
    path: req.file.path,
  });
});

export default uploadRouter;
